package com.andy.qhb.action;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.andy.qhb.entity.UsrQiangbLog;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.game.QhbGame;
import com.andy.qhb.game.Room;
import com.andy.qhb.utils.GmAction;
import com.andy.qhb.utils.SendUtil;
import com.andy.qhb.websocket.MySocketSession;

/**
 * 抢包
 * @author cuiwm
 *
 */
@Component(GmAction.ACTION_PREFIX+GmAction.ACTION_122)
public class Action122 implements IWebAction{

	@Override
	public void doAction(MySocketSession myWebSocket, Map<String, Object> data) {
		
		GameUser user = myWebSocket.getUser();
		Room room = user.getRoom();
		if(room == null)return;
		Integer packId = MapUtils.getInteger(data, "packId");
		
		QhbGame game = Global.getGame(packId);
		
		int[] coin = game.nextCoin(user);
		if(coin[1] > 0){
			UsrQiangbLog qiangbLog = new UsrQiangbLog();
			qiangbLog.setCoin(coin[1]);
			qiangbLog.setPid(packId.longValue());
			qiangbLog.setUid(user.getId());
			qiangbLog.setQorder(coin[0]);
			GameUtils.saveDb(qiangbLog);
			
			game.addQlog(qiangbLog);
			
			SendUtil.send122(room,qiangbLog,user.getNickName(),game.creater().getNickName());
			
			if(game.isEnd()){
				// 结算
				game.reward();
			}
		}
		
		
	}

	
	
	
}
